var BuMenId, BuMenName;
$.getJSON('BuMenTree.php', {
    CurPersonId: CurPersonId
  },
  function(data, textStatus, jqXHR) {
    $('#BuMenTree').append('<li><span class="folder" colID="' + data['tree'][0].id + '" level="' + data['tree'][0].level + '" title="' + data['tree'][0].BuMenName + '">' + data['tree'][0].BuMenName.substr(0, 9) + '</span><ul></ul></li>');
    var father_id_array = new Array(); //把所有目录的父目录编号存在数组里
    for (var i = 1; i < data['tree'].length; i++) {
      if (jQuery.inArray(data['tree'][i].FatherId, father_id_array) < 0) {
        father_id_array.push(data['tree'][i].FatherId);
      }
    }
    $(father_id_array).each(function(index1) { //按father_id_array数组的顺序从data['tree']中找到相应目录放到其父目录下
      $(data['tree']).each(function(index2) {
        if (data['tree'][index2].FatherId == father_id_array[index1]) {
          push_BuMenTree(data['tree'][index2].FatherId, data['tree'][index2].id, data['tree'][index2].BuMenName, data['tree'][index2].IsZhongDuan, data['tree'][index2].level);
        }
      })
    })

    $("#BuMenTree").treeview({
      collapsed: false,
      unique: true
      // persist: "cookie",
      // cookieId: "treeview-zhiwei"
    });
    var BuMenArray = [];
    for (let i = 0; i < data['BuMenArray'].length; i++) {
      BuMenArray.push(data['BuMenArray'][i].BuMenId);
    }
    $('.file').click(function() { //tree的click事件
      BuMenId = $(this).attr("colID");
      BuMenName = $(this).attr("title");
      // LoadingIndex = layer.load('', 0);
      if (BuMenArray.indexOf(BuMenId) != -1 || BuMenArray.indexOf('1') != -1) {
        $('.mail-body').load('YongGongJiaoShi_list.html');
      }
    })

  }
);

function push_BuMenTree(fid, selfid, foldername, IsZhongDuan, level) { //用来将某个目录放入其父目录下面
  if (Number(IsZhongDuan) == 0) {
    $("#BuMenTree").find('span').each(function() {
      if ($(this).attr("colID") == fid) {
        $(this).next().append('<li><span class="folder" colID="' + selfid + '" title="' + foldername + '" level="' + level + '">' + foldername.substr(0, 7) + '</span><ul></ul></li>');
      }
    })
  } else {
    $("#BuMenTree").find('span').each(function() {
      if ($(this).attr("colID") == fid) {
        $(this).next().append('<li><a href="javascript:" class="file" colID="' + selfid + '" title="' + foldername + '" level="' + level + '">' + foldername.substr(0, 7) + '</a></li>');
      }
    })
  }
}
